//
//  ZYStatusCell.swift
//  WBBeta1
//
//  Created by wustzhy on 16/8/13.
//  Copyright © 2016年 Yestin. All rights reserved.
//

import UIKit
import SnapKit

class ZYStatusCell: UITableViewCell {
    
    var bottomViewTopConstraint: Constraint?
    
    var statusViewModel:ZYStatusViewModel?{
        didSet{
            //1 原创视图赋值
            originalView.statusViewModel = statusViewModel

            //2 转发视图赋值
            //卸载约束
            bottomViewTopConstraint?.uninstall()
            //若有转发
            if statusViewModel?.statusModel?.retweeted_status != nil {
                //赋值
                retweetView.statusViewModel = statusViewModel
                //更新约束
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    bottomViewTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                //显示
                retweetView.hidden = false
            }else{
                //更新约束
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    bottomViewTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
                //隐藏
                retweetView.hidden = true
            }
            //3 底部视图赋值
            bottomView.statusViewModel = statusViewModel

        }
    }
    
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        backgroundColor = RGB(237, green:237,blue:237)
        
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        
        originalView.snp_makeConstraints { (make) -> Void in
            //设置cell的间距
            make.top.equalTo(contentView).offset(8)
            
            make.leading.trailing.equalTo(contentView)
//            make.bottom.equalTo(self)
            //高度 内部已设
        }
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
            //高度 内部已设
        }
        bottomView.snp_makeConstraints { (make) -> Void in
             make.leading.trailing.equalTo(contentView)
            bottomViewTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.height.equalTo(35)
        }
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(bottomView)
        }
    }
    
    //原创微博
    lazy var originalView:ZYStatusOriginalView = ZYStatusOriginalView()
    // 转发微博
    private lazy var retweetView: ZYStatusRetweetView = ZYStatusRetweetView()
    // 底部视图
    private lazy var bottomView: ZYStatusBottomView = ZYStatusBottomView()
}
